<?php
namespace app\api\validate;
use think\Validate;
use think\Db;
/**
 * Class CashValidate
 * @package app\api\validate
 * @author zhenHong~
 * 商户验证类
 */
class CashValidate extends Validate{
    protected $rule = [
        'account'     => ['require','regex' => '0?(13|14|15|17|18|19)[0-9]{9}','checkAccountUnique'],
        'contactnum'  => ['require'],
        'password'    => ['require','regex' => '/^[a-zA-Z0-9]{6,18}$/'],
        'province'    => ['require'],
        'city'        => ['require'],
        'area'        => ['require'],
        'legal_person'=> ['require'],
        'id_number'   => ['require','regex' => '/(^\d{15}$)|(^\d{17}([0-9]|X)$)/'],
        'email'       => ['email'],
        'name'        => ['require'],
        'a_id'        => ['integer','require'],
        'pagenum'     => ['integer'],
        'lon'         => ['between:0,180'],
        'lat'         => ['between:0,90'],
        'id'          => ['require'],
        'phone'       => ['require','regex' => '0?(13|14|15|17|18|19)[0-9]{9}'],
        'b_id'        => ['require'],
        't_id'        => ['require'],
        'm_id'        => ['require'],
        'b_name'      => ['require'],
        't_name'      => ['require'],
        'm_name'      => ['require'],
        'licenseplate'=> ['require'],
        'attestation_id'=> ['require'],
    ];

    protected $message = [
        'account.require' => '登陆账号不能为空',
        'account.regex' => '登录手机号格式不正确',
        'account.checkAccountUnique' => '商户已注册',
        'contactnum' => '商户联系电话不能为空',
        'password' => '密码由6~18字母加数字组成',
        'province' => '请选择省份',
        'city'     => '请选择城市',
        'area'     => '请选择地区',
        'legal_person' => '请填写法人姓名',
        'id_number' => '身份证号码格式不正确',
        'email'     => '邮箱格式不正确',
        'name'      => '商户名称不能为空',
        'a_id'      => '参数格式错误a_id',
        'pagenum'   => '参数格式错误pagenum',
        'lon'       => '经度参数错误',
        'lat'       => '纬度参数错误',
        'id'        => 'o_id不能为空',
        'phone.require'     => '手机号不能为空',
        'phone.regex'       => '手机格式不正确',
        'b_id'              => 'b_id参数错误',
        't_id'              => 't_id参数错误',
        'm_id'              => 'm_id参数错误',
        'b_name'            => 'b_name参数错误',
        't_name'            => 't_name参数错误',
        'm_name'            => 'm_name参数错误',
        'licenseplate'      => 'm_name参数错误',
        'attestation_id'    => '认证ids参数错误',
    ];

    protected $scene = [
        //添加商户
        'addCash' => ['account','contactnum','province','city','area','legal_person','id_number','email','name','lon','lat'],
        //删除商户
        'deletCash' => ['a_id'],
        // 商户登录
        'login' => ['account'=>['require','regex' => '0?(13|14|15|17|18|19)[0-9]{9}'],'password'],
        //查询商户
        'selectCash' => ['pagenum'=>'require'],
        // 商户发送obd给用户
        'sendObdToUser' => ['id','a_id','phone','b_id','t_id','m_id','b_name','t_name','m_name','licenseplate'],
        'bindUser'      => ['phone'],
        //  商户添加认证
        'addAttestation'        => ['attestation_id','a_id'],
        // 附近商户列表
        'nearCash'        => ['pagenum'=>'require','lat' => 'require','lon'=>'require'],
        // 修改商户
        'editCash' =>['contactnum','province','city','legal_person'],
        'editPassword' =>['password']

    ];

    /**
     * @param $value
     * @return bool
     * 校验账号的唯一性
     */
    protected function checkAccountUnique($value){
        $result = Db::name('cash')
            ->where('account',$value)
            ->value('account');
        return $result ? false : true;
    }
}